import Clain.FaultHandler;
import Clain.ObstacleHandler;
import Clain.SafetyHandler;
import Command.*;
import Clain.BoundaryViolationHandler;
import Observer.EnvironmentData;
import Observer.EnvironmentSensorNetwork;
import Strategy.PathPlanner;
import Strategy.StandardPathPlanning;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    private EnvironmentSensorNetwork sensorNetwork;
    private PathPlanner pathPlanner;
    private RemoteController remoteController;
    private SafetyHandler safetyHandler;
    private Scanner scanner;

    // 各功能管理器
    private MachineManager machineManager;
    private EnvironmentMonitor environmentMonitor;
    private PathPlanningManager pathPlanningManager;
    private TaskExecutionManager taskExecutionManager;
    private SystemManagement systemManagement;
    private EnvironmentData environmentData = new EnvironmentData();

    public Test() {
        // 初始化系统组件
        this.scanner = new Scanner(System.in);
        this.sensorNetwork = new EnvironmentSensorNetwork();
        this.pathPlanner = new PathPlanner(new StandardPathPlanning(environmentData));
        this.remoteController = new RemoteController();
        this.safetyHandler = setupSafetyHandlers();

        // 初始化农机列表
        List<Factory.AgriculturalMachine> agriculturalMachines = new ArrayList<>();

        // 初始化各功能管理器
        this.machineManager = new MachineManager(scanner, sensorNetwork, remoteController);
        this.environmentMonitor = new EnvironmentMonitor(scanner, sensorNetwork);
        // 传递农机列表给 PathPlanningManager 构造函数
        this.pathPlanningManager = new PathPlanningManager(
                scanner,
                pathPlanner,
                machineManager,  // 传递MachineManager实例
                environmentData  // 传递EnvironmentData实例
        );
        this.taskExecutionManager = new TaskExecutionManager(scanner, machineManager, remoteController, safetyHandler);
        this.systemManagement = new SystemManagement(scanner);
    }

    public void start() {
        System.out.println("=== 智能无人驾驶农机系统 ===");
        System.out.println("=== 主测试界面 ===");

        while (true) {
            displayMainMenu();
            int choice = getUserChoice(1, 6);

            switch (choice) {
                case 1:
                    machineManager.manage();
                    break;
                case 2:
                    environmentMonitor.manage();
                    break;
                case 3:
                    pathPlanningManager.manage();
                    break;
                case 4:
                    taskExecutionManager.manage();
                    break;
                case 5:
                    systemManagement.manage();
                    break;
                case 6:
                    System.out.println("退出系统...");
                    return;
            }
        }
    }

    private void displayMainMenu() {
        System.out.println("\n请选择功能:");
        System.out.println("1. 农机设备管理");
        System.out.println("2. 农田环境监测");
        System.out.println("3. 智能路径规划");
        System.out.println("4. 任务执行与控制");
        System.out.println("5. 系统管理");
        System.out.println("6. 退出系统");
    }

    private int getUserChoice(int min, int max) {
        while (true) {
            System.out.print("请输入选择(" + min + "-" + max + "): ");
            try {
                int choice = Integer.parseInt(scanner.nextLine());
                if (choice >= min && choice <= max) {
                    return choice;
                }
            } catch (NumberFormatException e) {
                System.out.println("无效输入，请重新选择!");
            }
        }
    }

    private SafetyHandler setupSafetyHandlers() {
        // 传递remoteController实例给构造函数
        SafetyHandler obstacleHandler = new ObstacleHandler(remoteController);
        SafetyHandler faultHandler = new FaultHandler(remoteController);
        SafetyHandler boundaryHandler = new BoundaryViolationHandler(remoteController);

        obstacleHandler.setNextHandler(faultHandler);
        faultHandler.setNextHandler(boundaryHandler);

        return obstacleHandler;
    }
    public static void main(String[] args) {
        Test ui = new Test();
        ui.start();
    }
}